<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>
        <c:if test="${empty(company.id)}">添加企业</c:if>
        <c:if test="${!empty(company.id)}">编辑企业</c:if>
    </title>

    <script type="text/javascript" charset="utf-8" src="${ctx}/static/inspinia/js/plugins/ueditor-1.4.3.3/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx}/static/inspinia/js/plugins/ueditor-1.4.3.3/_examples/editor_api.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx}/static/inspinia/js/plugins/ueditor-1.4.3.3/lang/zh-cn/zh-cn.js"></script>

</head>

<body>

<div id="wrapper">
    <div class="gray-bg">
        <div class="row m-t-sm">
            <div class="col-lg-12">
                <ol class="breadcrumb f-fl">
                    <li>
                        <i class="fa fa-location-arrow"></i><a>${sys_flag ? '企业管理' : '企业信息'}</a>
                    </li>
                    <c:if test="${sys_flag}">
                        <li class="active">
                            <strong>
                                <c:if test="${empty(company.id)}">添加企业</c:if>
                                <c:if test="${!empty(company.id)}">编辑企业</c:if>
                            </strong>
                        </li>
                    </c:if>
                </ol>
                <c:if test="${sys_flag}">
                    <span class="navbar-right  m-r m-t-8"><a href="javascript:history.go(-1);" class="btn-primary btn-add "> <i class='fa fa-mail-reply'></i> 返回</a></span>
                </c:if>
            </div>
        </div>
        <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-content">
                            <form method="get"
                                  <c:if test="${sys_flag}">data-ajax="${ctx}/admin/ehm/company/list" </c:if>
                                  <c:if test="${!sys_flag}">data-ajax="${ctx}/admin/ehm/company/info/edit" </c:if>
                                  action="${ctx}/admin/ehm/company/save"  class="form-horizontal" id="user_form">
                                <input  type="hidden" id="ids" name="id" value="${company.id}"/>
                               <%-- <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger">*</span>企业编号</label>
                                    <div class="col-sm-3">
                                        <input id="xmbh" type="text" class="form-control" onBlur="validate(this.value);" placeholder="编号" value="${company.sn}" name="sn" required <c:if test="${!sys_flag}">readonly</c:if>>
                                        <span id="userText"></span>
                                    </div>
                                </div>--%>
                                <div class="form-group"><label class="col-sm-2 control-label"><span class="text-danger">*</span>企业名称</label>
                                    <div class="col-sm-8"><input type="text" class="form-control" placeholder="请输入企业名称" value="${company.title}" name="title" required <c:if test="${!sys_flag}">readonly</c:if>></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">图片</label>
                                    <div class="col-sm-2">
                                        <input  type="hidden" id="src" name="image" value="${company.image}"/>
                                        <div class="imager" id="srcView">
                                            <c:choose>
                                                <c:when test="${not empty company.image}">
                                                    <img src="${company.image}" width="100%" height="100%">
                                                </c:when>
                                                <c:otherwise>
                                                    <img src="${ctx}/static/inspinia/img/default_pic_no.jpg" width="100%" height="100%">
                                                </c:otherwise>
                                            </c:choose>
                                        </div>
                                    </div>
                                    <div class="col-sm-3 upload-img-pt25">
                                        <div class="upload f-fl" id="uploaderLogoUrl"></div>
                                        <div class="tip f-fl upload-img-pt10"><span class="upload-img-ml6">建议上传：676*520</span></div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger">*</span>所在地区</label>
                                    <input type='hidden' id="provinceId" name='provinceId' value="${company.provinceId}"/>
                                    <div class="col-sm-2">
                                        <select class="form-control" name="province" id="province" required>

                                        </select>
                                    </div>
                                    <input type='hidden' id="cityId" name='cityId' value="${company.cityId}"/>
                                    <div class="col-sm-2">
                                        <select class="form-control" name="city" id="city" required>

                                        </select>
                                    </div>
                                    <input type='hidden' id="districtId" name='countryId' value="${company.countryId}"/>
                                    <div class="col-sm-2">
                                        <select class="form-control" name="country" id="district" required>

                                        </select>
                                    </div>
                                </div>
                                <div class="form-group"><label class="col-sm-2 control-label">详细地址</label>
                                    <div class="col-sm-8"><input type="text" class="form-control" placeholder="请输入详细地址" value="${company.address}" name="address" ></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger">*</span>联系人</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入联系人" value="${company.contact}" name="contact" required></div>

                                    <label class="col-sm-2 control-label"><span class="text-danger">*</span>联系电话</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入联系电话" value="${company.phone}" name="phone" required></div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger"></span>机构代码</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入机构代码" value="${company.code}" name="code" required></div>

                                    <label class="col-sm-2 control-label"><span class="text-danger"></span>企业传真</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入传真" value="${company.fax}" name="fax" ></div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger"></span>企业邮箱</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入邮箱" value="${company.emails}" name="emails" ></div>

                                    <label class="col-sm-2 control-label"><span class="text-danger"></span>企业网址</label>
                                    <div class="col-sm-3"><input type="text" class="form-control" placeholder="请输入网址" value="${company.website}" name="website" ></div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><span class="text-danger">*</span>登录账号</label>
                                    <div class="col-sm-8"><input type="text" class="form-control" placeholder="请输入登录账号" value="${company.username}" name="username" required <c:if test="${company.id != null}">readonly</c:if>></div>
                                </div>
                                <c:if test="${!empty(company.id)}">
                                    <div class="form-group"><label class="col-sm-2 control-label">密码</label>
                                        <div class="col-sm-6"><input type="button" class="btn btn-white" value="重置" onclick="resetPass()"> <label class="control-label" style="font-weight:100; padding-left: 10px;">重置后密码 "123456"</label></div>
                                    </div>
                                </c:if>
                                <div class="form-group"><label class="col-sm-2 control-label">角色</label>
                                    <div class="col-sm-6 select-role-box">
                                        <select id="selectRole" class="select2_role form-control" name="roles" multiple="multiple" required ${!sys_flag ? 'disabled' : ''}>
                                            <c:forEach var="role" items="${roles}">
                                                <option value="${role.roleId}">${role.roleName}</option>
                                            </c:forEach>
                                        </select>

                                    </div>
                                </div>
                                <div class="form-group" id="newsdescDiv">
                                    <label class="col-sm-2 control-label">企业简介</label>
                                    <div class="col-sm-8"><textarea name="memo" id="editor" type="text/plain" style="width:100%;height:444px;">${company.memo}</textarea></div>
                                </div>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="save()">保 存</button>
                                        <a href="javascript:history.go(-1);" class="btn btn-white ">  取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script type="text/javascript" src="${ctx}/static/inspinia/js/plugins/LD.sot/multiupf/uploadify2/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="${ctx}/static/inspinia/js/plugins/LD.sot/multiupf/uploadify2/swfobject.js"></script>
<script type="text/javascript" src="${ctx}/static/inspinia/js/plugins/LD.sot/multiupf/multiupf.uploadify2.js"></script>
<script>
    var id ;
    var flag = false;
    $(document).ready(function() {
        var ue = UE.getEditor('editor');
        init_src();
        init_province();

        id = $("#ids").val();
        if (id == '' || id == undefined) {
            id = 0;
        }
        var select2_role =  $(".select2_role").select2();
        <c:if test="${company.id !=null}">
        var rids="${rids}";
        select2_role.val(eval("(["+rids+"])")).trigger('change');
        </c:if>
    });

    function init_src() {
        init_uploader('uploaderLogoUrl', 'srcView', 'src');
    }
    function init_uploader(id, view_id, url_id,del_btn) {
        $('#'+id).multiupf({
            uploader: '${image_upload_url}',
            fileExts: '*.png;*.jpg;', fileDesc: '*.png;*.jpg;',
            progress: function (data, queue) {
                if (queue.indexOf(id) > -1) {
                    $('#'+view_id).html("上传中...");
                }
            },
            complete: function (result, queue) {
                var url = result.data[0];
                if (queue.indexOf(id) > -1) {
                    $('#'+view_id).html('<img data-img src="' + url + '"  width="100%" height="100%">');
                    $('#'+url_id).val(url);
                    $("#"+del_btn).show();
                }
            }
        });
    }

    function init_province() {
        var province = $("#province");
        var provinceId = $("#provinceId");
        var city = $("#city");
        var cityId = $("#cityId");
        var district = $("#district");
        var districtId = $("#districtId");
        province.append("<option value=''>请选择省份</option>");
        $.ajax({
            url: "${ctx}/admin/bas/region/list",
            method: "POST",
            data: {level: 2},
            dataType: "json",
            async: false,
            success: function(response) {
                if (response.success) {
                    var data = response.data;
                    for (var i = 0; i < data.length; i ++) {
                        var option = "<option id='" + data[i].id + "' value='" + data[i].title + "'>" + data[i].title + "</option>";
                        province.append(option);
                    }
                    if ("${company.id}" != "") {
                        province.find("option[value='${company.province}']").attr("selected", true);
                    }
                }
            }
        });
        province.change(function() {
            $.ajax({
                url: "${ctx}/admin/bas/region/list",
                method: "POST",
                data: {level: 3, pid: province.find("option:selected").attr("id")},
                dataType: "json",
                success : function(response) {
                    city.empty();
                    city.append("<option value=''>请选择城市</option>");
                    var data = response.data;
                    for (var i = 0; i < data.length; i ++) {
                        var option = "<option id='" + data[i].id + "' value='" + data[i].title + "'>" + data[i].title + "</option>";
                        city.append(option);
                    }
                    provinceId.val(province.find("option:selected").attr("id"));
                }
            });
        });
        city.change(function() {
            $.ajax({
                url: "${ctx}/admin/bas/region/list",
                method: "POST",
                data: {pid: city.find("option:selected").attr("id")},
                dataType: "json",
                success : function(response) {
                    district.empty();
                    district.append("<option value=''>请选择地区</option>");
                    var data = response.data;
                    for (var i = 0; i < data.length; i ++) {
                        var option = "<option id='" + data[i].id + "' value='" + data[i].title + "'>" + data[i].title + "</option>";
                        district.append(option);
                    }
                    cityId.val(city.find("option:selected").attr("id"));
                }
            });
        });

        district.change(function() {
            districtId.val(district.find("option:selected").attr("id"));
        });

        if ("${company.id}" != "") {
            $.ajax({
                url: "${ctx}/admin/bas/region/list",
                method: "POST",
                data: {level: 3, pid: provinceId.val()},
                dataType: "json",
                success : function(response) {
                    city.empty();
                    city.append("<option value=''>请选择城市</option>");
                    var data = response.data;
                    for (var i = 0; i < data.length; i ++) {
                        var option = "<option id='" + data[i].id + "' value='" + data[i].title + "'>" + data[i].title + "</option>";
                        city.append(option);
                    }
                    city.find("option[id='${company.cityId}']").attr("selected", true);
                }
            });
            $.ajax({
                url: "${ctx}/admin/bas/region/list",
                method: "POST",
                data: {pid: cityId.val()},
                dataType: "json",
                success : function(response) {
                    district.empty();
                    district.append("<option value=''>请选择地区</option>");
                    var data = response.data;
                    for (var i = 0; i < data.length; i ++) {
                        var option = "<option id='" + data[i].id + "' value='" + data[i].title + "'>" + data[i].title + "</option>";
                        district.append(option);
                    }
                    district.find("option[id='${company.countryId}']").attr("selected", true);
                }
            });
        }
    }

    function resetPass() {
        parent.layer.confirm('是否确认重置密码？', function() {
            $.ajax({
                url: "${ctx}/admin/ehm/company/resetPass",
                method: "POST",
                data: {username : "${company.username}"},
                dataType: "json",
                success : function(response) {
                    if (response.success) {
                        parent.layer.alert('重置成功');
                    } else {
                        parent.layer.alert('重置失败');
                    }
                }
            });
        })
    }

    function save() {
        var b = true;
        var provinceId = $("#provinceId");
        var cityId = $("#cityId");
        var districtId = $("#districtId");
        if (provinceId.val() == '' || provinceId.val() == undefined) {
            parent.layer.alert('请选择省份');
            return;
        }
        if (cityId.val() == '' || cityId.val() == undefined) {
            parent.layer.alert('请选择城市');
            return;
        }
        if (districtId.val() == '' || districtId.val() == undefined) {
            parent.layer.alert('请选择地区');
            return;
        }

        if(flag){
            parent.layer.alert('编码重复');
            return;
        }
        $("#selectRole").removeAttr("disabled");
        if (b) {
            $("#user_form").submit();
        }
    }

    function validate(val) {

        if(val == '' || val == undefined){
            parent.layer.msg('请输入编码');
            return;
        }else{
            $.ajax({
                method: "POST",
                url: "${ctx}/admin/ehm/company/validations",
                data: {
                    validation:val,
                    id:id
                },
                dataType: "json",
                async: false,
                success: function(response) {
                    if (response.code == 0) {
                        if(response.msg == '该编码已被占用'){
                            parent.layer.msg('该编码已被占用,请重新输入编码');
                            flag = true;
                        }else{
                            flag = false;
                        }
                        console.log(response.msg);
                    }else{
                        flag = false;
                    }
                }
            });
        }

    }

</script>
</body>

</html>
